import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:grab_the_order/core/values/app_styles.dart';
import 'home_controller.dart';

class HomeView extends GetView<HomeController> {
  const HomeView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('抢单控制台'),
      ),
      body: Container(
        decoration: BoxDecoration(
            gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: [
            kBackgroundColor,
            Colors.white,
          ],
        )),
        child: SafeArea(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              _buildStatusPanel(),
              _buildActionButton(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildStatusPanel() {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 24),
      padding: const EdgeInsets.symmetric(vertical: 24),
      decoration: modernDecoration(),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Expanded(
              child: _buildStatusItem("今日战果", controller.ordersGrabbedToday)),
          Expanded(
              child: _buildStatusItem("运行时长", controller.runningDuration,
                  isTime: true)),
        ],
      ),
    );
  }

  Widget _buildStatusItem(String title, dynamic value, {bool isTime = false}) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          title,
          style: TextStyle(
            color: Colors.black.withOpacity(0.6),
            fontWeight: FontWeight.w600,
            fontSize: 16,
          ),
        ),
        const SizedBox(height: 12),
        Obx(() {
          String displayValue = isTime
              ? (value as RxString).value
              : (value as RxInt).value.toString();
          return Text(
            displayValue,
            style: TextStyle(
              color: Colors.black.withOpacity(0.8),
              fontWeight: FontWeight.bold,
              fontSize: isTime ? 28 : 36,
            ),
          );
        }),
      ],
    );
  }

  Widget _buildActionButton() {
    return Obx(() {
      bool isRunning = controller.isServiceRunning.value;
      IconData icon = isRunning
          ? Icons.stop_circle_outlined
          : Icons.play_circle_fill_outlined;
      String text = isRunning ? "停止服务" : "启动服务";

      return GestureDetector(
        onTap: controller.toggleService,
        child: AnimatedContainer(
          duration: const Duration(milliseconds: 300),
          width: Get.width * 0.55,
          height: Get.width * 0.55,
          decoration: modernDecoration(
            radius: Get.width * 0.3,
            hasShadow: !isRunning,
          ).copyWith(
            gradient: kGradientButton,
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(icon, color: Colors.white, size: Get.width * 0.2),
              const SizedBox(height: 10),
              Text(
                text,
                style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                    fontSize: Get.width * 0.05),
              ),
            ],
          ),
        ),
      );
    });
  }
}
